package com.ai.cmdc.codeutil.export_generator;

import com.ai.cmdc.codeutil.bean.BeanConvertor;
import com.ai.cmdc.codeutil.bean.ExportColumn;
import com.ai.cmdc.codeutil.bean.ExportColumnOPo;
import com.ai.cmdc.codeutil.export_generator.dao.QryTableMapper;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhoulk
 * @Description:
 * @date: 2019/8/15
 */
@Service
public class TableExportGenerator {

    @Autowired
    QryTableMapper tableMapper;

    public void execution(String table, String fileDir) throws IOException {
        File file = new File(fileDir + table + ".xlsx");
        if (!file.exists()) {
            if (!file.createNewFile()) {
                return;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        List<ExportColumn> lists1 = tableMapper.qryColumn(table);
        List<ExportColumn> lists2 = tableMapper.qryColumnComment(table);
        Map<String, String> commentMap = lists2.stream().filter(v -> v.getComments() != null).collect(Collectors.toMap(ExportColumn::getColumnName, ExportColumn::getComments));
        List<ExportColumnOPo> list = BeanConvertor.copyList(lists1, ExportColumnOPo.class);
        int i = 1;
        for (ExportColumnOPo v : list) {
            v.setIndex(i++);
            //格式化字段长度
            if (v.getDataType().equals("NUMBER")) {
                v.setDataLength("V" + v.getDataPrecision());
            } else if (v.getDataType().equals("DATE")) {
                v.setDataLength("V" + 1);
            } else {
                v.setDataLength("V" + v.getDataLength());
            }
            //获取字段名
            v.setComments(commentMap.get(v.getColumnName()));
            //格式化字段类型
            v.setDataType(formatterDataType(v.getDataType(), v.getDataPrecision()));
            //格式化列名
            String name = v.getColumnName();
            String[] strs = name.split("_");
            name = strs[0].toLowerCase();
            for (int j = 1; j < strs.length; j++) {
                String str = strs[j];
                str = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
                name = name + str;
            }
            v.setColumnName(name);
            //格式化注释
            String comment = v.getComments();
            if (comment != null) {
                strs = comment.split(":").length > 1 ? comment.split(":") : comment.split("：");
                v.setVar1(strs.length > 1 ? strs[1] : null);
                v.setComments(strs[0]);
            }
        }
        EasyExcelFactory.writeExcel(fileOutputStream, "政策", ExcelTypeEnum.XLSX, list, ExportColumnOPo.class, null);

    }

    private String formatterDataType(String type, String dataPrecision) {
        if (type.equals("VARCHAR2")) {
            return "String";
        }
        if (type.equals("NUMBER")) {
            int len = Integer.valueOf(dataPrecision);
            if (len <= 2) {
                return "Short";
            }
            if (len <= 6) {
                return "Integer";
            }
            if (len <= 16) {
                return "Long";
            }
        }
        if (type.equals("DATE")) {
            return "Date";
        }
        return type;
    }
}
